package com.aiti.lulian.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.aiti.base.core.base.service.iml.BaseSerivceImpl;
import com.aiti.lulian.AdCache;
import com.aiti.lulian.entity.LuLianDockingMeetingAgenda;
import com.aiti.lulian.entity.LuLianDockingMeetingDetails;
import com.aiti.lulian.entity.LuLianDockingMeetingRoom;
import com.aiti.lulian.entity.NameAndValue;
import com.aiti.lulian.mapper.LuLianDockingMeetingAgendaMapper;
import com.aiti.lulian.mapper.LuLianDockingMeetingDetailsMapper;
import com.aiti.lulian.mapper.LuLianDockingMeetingRoomMapper;
import com.aiti.lulian.service.ILuLianDockingMeetingDetailsService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author WongMZ
 * @CreateDate 2024-05-28
 * @Description: LuLianDockingMeetingDetails服务实现类
 */
@Service
public class LuLianDockingMeetingDetailsServiceImpl extends BaseSerivceImpl<LuLianDockingMeetingDetailsMapper, LuLianDockingMeetingDetails> implements ILuLianDockingMeetingDetailsService {
    @Autowired
    private LuLianDockingMeetingDetailsMapper detailsMapper;

    @Autowired
    private LuLianDockingMeetingAgendaMapper agendaMapper;

    @Autowired
    private AdCache adCache;

    @Autowired
    private LuLianDockingMeetingRoomMapper meetingRoomMapper;

    @Override
    public LuLianDockingMeetingDetails getDetailsByBaseId(String baseId) {
//        LuLianDockingMeetingDetailsVo detailsVo = new LuLianDockingMeetingDetailsVo();//创建一个空的供需对接会详情的Vo对象

        LuLianDockingMeetingDetails details = detailsMapper.selectDetailsByBaseId(baseId);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String format = simpleDateFormat.format(details.getMeetingDate());
        details.setMeetingDateStr(format);

        LambdaQueryWrapper<LuLianDockingMeetingRoom> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(LuLianDockingMeetingRoom::getActivityId, baseId);
        LuLianDockingMeetingRoom meetingRoom = meetingRoomMapper.selectOne(wrapper);
        if (meetingRoom != null && BeanUtil.isNotEmpty(meetingRoom)) {
            details.setRoomId(meetingRoom.getRoomId());
        }
//        BeanUtils.copyProperties(details, detailsVo);//进行除list之外其他属性的复制赋值
        if (details != null) {
            List<LuLianDockingMeetingAgenda> list = agendaMapper.selectAgendaByMeetingId(baseId);
            List<Map<String, String>> resList = new ArrayList<>();
            SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("HH:mm");
            for (LuLianDockingMeetingAgenda agenda : list) {
                Map<String, String> map = new HashMap<>();
                map.put("agendaBaseId", agenda.getAgendaBaseId());
                map.put("meetingId", agenda.getMeetingId());
                map.put("startTime", simpleDateFormat1.format(agenda.getStartTime()));
                map.put("endTime", simpleDateFormat1.format(agenda.getEndTime()));
                map.put("description", agenda.getDescription());
                map.put("sort", agenda.getSort());
                resList.add(map);
            }
            details.setFormatAgendaList(resList);
        }
        return details;
    }

    @Override
    public Map<String, Object> getDetailsListPage(Integer pageNum, Integer pageSize, String meetingStatus, String xcVideo,String keyword, String govFlag, String city, String county) {
        Map<String, Object> map = new HashMap<>();

        Page<LuLianDockingMeetingDetails> page = new Page<>(pageNum, pageSize);
        //创建一个条件构造器并设置按照活动时间（meetingDate）降序排列
        LambdaQueryWrapper<LuLianDockingMeetingDetails> queryWrapper = new LambdaQueryWrapper<>();
        if (StrUtil.isNotBlank(meetingStatus)) {
            queryWrapper.eq(LuLianDockingMeetingDetails::getMeetingStatus, meetingStatus);
        }
        if (StrUtil.isNotBlank(xcVideo)) {
            queryWrapper.eq(LuLianDockingMeetingDetails::getXcVideo, xcVideo);
        }
        if(StrUtil.isNotBlank(keyword)){
            queryWrapper.like(LuLianDockingMeetingDetails::getActivityName,keyword);
        }
        if(StrUtil.isNotBlank(govFlag)){
            queryWrapper.like(LuLianDockingMeetingDetails::getGovFlag,govFlag);
        }else{
            queryWrapper.like(LuLianDockingMeetingDetails::getGovFlag,"0");
        }
        if (StrUtil.isNotBlank(city)) {
            queryWrapper.eq(LuLianDockingMeetingDetails::getCity, city);
        }
        if (StrUtil.isNotBlank(county)) {
            queryWrapper.eq(LuLianDockingMeetingDetails::getCounty, county);
        }
        queryWrapper.orderByDesc(LuLianDockingMeetingDetails::getMeetingDate);

        IPage<LuLianDockingMeetingDetails> pageResult = detailsMapper.selectPage(page, queryWrapper);
        List<LuLianDockingMeetingDetails> list = pageResult.getRecords();
        int total = (int) pageResult.getTotal();

        for (LuLianDockingMeetingDetails item : list) {
            String itemCity = item.getCity();
            item.setCity(adCache.adCache.get(itemCity));
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            item.setMeetingDateStr(simpleDateFormat.format(item.getMeetingDate()));
        }

        map.put("total", total);
        map.put("list", list);

        return map;
    }

    @Override
    public List<Map<String,Object>> queryMeetingEnroll(String userId) {
        //查询当前用户下创建的活动
        List<LuLianDockingMeetingDetails> meetingDetails = detailsMapper.selectList(new QueryWrapper<LuLianDockingMeetingDetails>().eq("base_create_by", userId));

        List<Map<String,Object>> result = new ArrayList<>();
        //查询供应商报名数据
        Map<String,Integer> cgsMap = new HashMap<>(16);
        Map<String,Integer> gysMap = new HashMap<>(16);
        if(meetingDetails != null && meetingDetails.size() > 0){
            List<String> collect = meetingDetails.stream().map(LuLianDockingMeetingDetails::getBaseId).collect(Collectors.toList());
            List<NameAndValue> cgsCount = detailsMapper.queryCgsCount(collect);
            cgsMap = cgsCount.stream().collect(Collectors.toMap(NameAndValue::getName,NameAndValue::getValue));

            List<NameAndValue> gysCount = detailsMapper.queryGysCount(collect);
            gysMap = gysCount.stream().collect(Collectors.toMap(NameAndValue::getName,NameAndValue::getValue));

            for(LuLianDockingMeetingDetails item : meetingDetails){
                String baseId = item.getBaseId();
                Map<String,Object> itemMap = new HashMap<>(16);

                //当前会议供应端
                itemMap.put("activityName",item.getActivityName());
                itemMap.put("gysCount",gysMap.get(baseId) == null ? 0 : gysMap.get(baseId));
                itemMap.put("cgsCount",cgsMap.get(baseId) == null ? 0 : cgsMap.get(baseId));
                result.add(itemMap);
            }
        }
        return result;
    }
}